package com.lanhao.aot.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.apache.commons.io.IOUtils;

import com.beust.jcommander.internal.Maps;

import cn.hutool.core.io.IoUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

public class SseLingXiUtil {
	
	public static void main(String[] args) throws IOException {
//		Map<String, String> header = Maps.newHashMap();
//		header.put("authToken", "_HHUDZZRBYsbphJGrCUsx78yJ-u2xNAHjTFl8xTXLh02kogj1fgM4XZhpOZrWQtX");
//		header.put("sdkAppKey", "zgydApp");
//		header.put("Origin", "https://cmkf.cmcc-cs.cn");
//		header.put("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
//		header.put("Content-Type", "application/json");
//		HttpResponse resp = HttpRequest.post("https://h.app.coc.10086.cn/lingxi/api/tmp/flow").addHeaders(header).execute();
//		
//		BufferedReader reader = new BufferedReader(new InputStreamReader(resp.bodyStream()));
//		String line;
//		while ((line = reader.readLine()) != null) {
//			System.out.println(line);
//		}
//		reader.close();
		
		String urlAddr = "https://h.app.coc.10086.cn/lingxi/api/tmp/flow";
        BufferedReader reader = null;
        try {
            URL url = new URL(urlAddr);
            // 建立链接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Accept", "text/event-stream");
            connection.setRequestProperty("Content-type", "application/json; charset=UTF-8");
            connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("Connection", "keep-alive");
            
            connection.setRequestProperty("authToken", "_HHUDZZRBYsbphJGrCUsx78yJ-u2xNAHjTFl8xTXLh02kogj1fgM4XZhpOZrWQtX");
            connection.setRequestProperty("sdkAppKey", "zgydApp");
            connection.setRequestProperty("Origin", "https://cmkf.cmcc-cs.cn");
            // 允许输入和输出
            connection.setDoInput(true);
            connection.setDoOutput(true);
            // 设置超时为0，表示无限制
            connection.setConnectTimeout(0);
            connection.setReadTimeout(0);
            // 传参
            String params = "";
            // 写入POST数据
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(params.getBytes(StandardCharsets.UTF_8));
            out.flush();
            out.close();
 
            // 读取SSE事件
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            StringBuilder eventBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            reader.close();
            // 断开链接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(reader);
        }
        
	}
}
